package online.westonline.com.westonline.ui;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;


import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.base.BaseActivity;
import online.westonline.com.westonline.utils.ContactsUtils;
import online.westonline.com.westonline.utils.ToastUtils;
import pub.devrel.easypermissions.EasyPermissions;

public class QeActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks{


    @BindView(R.id.zx_view)
    ZXingView zxView;

    @Override
    protected void initview() {
    //检测权限
        check();
    }

    private void check() {
        String[] per = new String[]{Manifest.permission.CAMERA};
        if (EasyPermissions.hasPermissions(this,per))
        {
            //开启摄像头
            initzx();
        }
        else
        {
            EasyPermissions.requestPermissions(this,"申请读取相机权限",10,per);
        }
    }

    private void initzx() {
        zxView.startCamera();
        zxView.startSpotAndShowRect();
        zxView.startSpotDelay(1000);
        zxView.setDelegate(new QRCodeView.Delegate() {
            @Override
            public void onScanQRCodeSuccess(String result) {
                try {
                    if (!TextUtils.isEmpty(result))
                    {
                        //获取到了,去往新建联系人界面
                        String phone = result.split(":")[0];
                        String name = result.split(":")[1];
                        ContactsUtils.savePeople(phone,name,QeActivity.this);
                        Log.e("提示","到这里了");
                    }
                }catch (Exception e)
                {
                    ToastUtils.showToast("二维码错误");
                }
            }

            @Override
            public void onScanQRCodeOpenCameraError() {
                ToastUtils.showToast("识别失败");
                Log.e("提示","到这里了");
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        zxView.stopCamera();
        zxView.stopSpotAndHiddenRect();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initzx();
    }
    @Override
    protected void initdata() {

    }

    @Override
    protected int setlayout() {
        return R.layout.activity_qe;
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        //开启摄像头()
            Intent intent = getIntent();
            finish();
            startActivity(intent);

    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this);
    }

}
